package com.mingtu.guards.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.utils.SPTools;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.utils.MyConstant;
import com.mingtu.guards.view.SlideFinishLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

public class LockScreenNewActivity extends MyBaseActivity {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.slide_finish_layout)
    SlideFinishLayout slideFinishLayout;
    @BindView(R.id.tv_mileage)
    TextView tvMileage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {

        return R.layout.activity_locked_new;
    }

    @Override
    protected void initView() {
        hideHeadView();
        //沉侵式状态栏
        ImmersionBar immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarDarkFont(false);  //状态栏字体是深色，不写默认为亮色
        immersionBar.navigationBarDarkIcon(false);
        immersionBar.init();

        slideFinishLayout.setEnableRightSlideEvent(false);
        slideFinishLayout.setOnSlideFinishListener(new SlideFinishLayout.OnSlideFinishListener() {
            @Override
            public void onSlideBack() {
                finish();
            }

            @Override
            public void onSlideForward() {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void getData() {
        String distance = SPStaticUtils.getString(SPTools.distance);
        if (!StringUtils.isEmpty(distance)) {
            tvMileage.setText(distance + "");
        }
    }

    @Override
    public void onBackPressed() {
        // 锁屏界面当然不响应Back按键, 只需要重写Activity的onBackPressed方法即可
        // super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int key = event.getKeyCode();
        switch (key) {
            case KeyEvent.KEYCODE_BACK:
                return true;

            case KeyEvent.KEYCODE_MENU:
                return true;


        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //当用户处在次页面的时候，联网之后刷新数据
        String message = event.getMessgae();
        if (message.equals(MyConstant.LOCK_SCREEN_MILEAGE_DATA)) {
            String milleage = event.getParam();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMileage.setText(milleage + "");
                }
            });
        } else if (message.equals(MyConstant.LOCK_SCREEN_TIME_DATA)) {
            String time = event.getParam();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvTime.setText(time + "");
                }
            });
        }

    }

}